import * as echarts from '../../../components/ec-canvas/echarts';
import Toast from '@vant/weapp/toast/toast';
const app = getApp()
const api = app.globalData.api
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ec: {
      lazyLoad: true
    },
    hallOption:[],
    hall_popup_show:false,
    hallName:'',
    hallId:'',
    active:0,
    time:'',
    show:false,
    menu:[],
    weeArray:['日','一','二','三','四','五','六'],
    dayArr:[],
    type_active:0,//餐别
    selected_Date:'',
    selected_month:'',
    formate_selected_month:'',
    formate_selected_date:'',
    today:'',//当前选中的第几个日期
    latitude:'',//纬度
    longitude:'',//经度
    clockPageInfo:{},
    startSignTime:'',
    endSignTime:'',
    mealTypeOption:['早餐','午餐','晚餐','宵夜'],
    person:{},
    nowMealType:'早餐',//当前餐别
    isReport:0,//是否已报餐
    record:{},
    func_menu:[],
    meal_collection:[],//统计数组
    xData:[],
    yData:[],
  },
  initCharts: function () {
    let that = this;
    that.ecComponent.init((canvas, width, height, dpr) => {
      // 获取组件的 canvas、width、height 后的回调函数
      // 在这里初始化图表
      const chart2 = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr // new
      });
      // let data = [value, value, value, ];
      chart2.setOption(that.getBarOption(that.data.xData,that.data.yData));
      // 将图表实例绑定到 this 上，可以在其他成员函数（如 dispose）中访问
      that.chart2 = chart2;
     
      // 注意这里一定要返回 chart 实例，否则会影响事件处理等
      return chart2;
    });
   
  },
  getBarOption(xData,yData){
    let option = {
      tooltip:{
        show:true,
       },
       legend: {
        show:true,
      },
      xAxis: {
        type: 'category',
        data: xData
      },
      yAxis: {
        type: 'value'
      },
      series: yData
    };
     return option
 },
  navigateTo(e){
    let name = e.currentTarget.dataset.name;
    if(name ==='报餐'){
    
      this.setData({
        active: 0
      });
      this.requestData();
    }else if(name=='报餐取餐'){
      this.setData({
        active: 2
      });
      this.requestData();
    }
  },
  /**取消报餐 */
  cancelReport(){
    let that = this;
    let id = !api.isBlank(this.data.record.id)?this.data.record.id:'';
    if(!id){
      wx.showToast({
        icon:'none',
        title: '您还没有报餐',
      })
      return false;
    }
    api.curlPost('?cla=canteenApp&fun=cancelReport',{id:id}).then((res)=>{
      Toast(res.warn);
      that.requestData();
    })

  },

  /**餐别切换 */
  onMealChange(event) {
    this.setData({
      nowMealType:event.detail.name,
      type_active:event.detail.index
    })
    this.requestData();
  },
  /***关闭弹出层 */
  onClose(e){
    this.requestData();
    this.setData({
      show:false
    })
  },
  /**tabar切换 */
  onChange(event){
    let index = event.detail;
    this.setData({
      active: index
    });
    this.requestData('',true);
  },
  //获取当前时分秒
  getTime(){
    this.setData({
      time:api.formatDateTime(new Date(),'HH:mm:ss')
    })
  },
  chooseDate(e){
    this.setData({
      formate_selected_date:e.currentTarget.dataset.date
    })
    this.requestData();
  },
  changeMonth(e){
    let type = e.currentTarget.dataset.type;
    let month = this.data.formate_selected_month;
  
    if(type=='pre'){
      month=api.getPreMonth(api.formatDateCRM('Y/m',month));
    }else if(type=='next'){
      month=api.getNextMonth(api.formatDateCRM('Y/m',month));
    }
    this.requestData(month);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
   
    setTimeout(function () {    
     
      that.requestData();
    }, 1000)
   
   
  },

  /**请求数据 */
  requestData(month='',isInit=false){
    let that = this;
    if(this.data.active==0){
      let para = {
        date:isInit?'':that.data.formate_selected_date,
        month:isInit?'':month,
        mealType:isInit?'':that.data.nowMealType
      }
      api.curlPost('?cla=canteenApp&fun=mealReport',para).then((res)=>{
        that.setData({
          mealTypeOption:res.data.result.mealOption,
          menu:res.data.result.menu,
          dayArr:res.data.result.list,
          selected_month:api.formatDateCRM('Y年m月',res.data.result.month),
          formate_selected_month:api.rowTocol(res.data.result.month),
          formate_selected_date:res.data.result.date,
          person:res.data.result.person,
          isReport:parseInt(res.data.result.isReport),
          nowMealType:res.data.result.nowMealType,
          record:res.data.result.record    
        })
      })
    }else if(this.data.active == 1){
      api.curlPost('?cla=canteenApp&fun=func').then((res)=>{
        that.setData({
          func_menu:res.data.result.menu,
        })
      })
    }else if(this.data.active == 2){
      that.ecComponent = that.selectComponent('#canvas');
      let para = {
        date:isInit?'':that.data.formate_selected_date,
        month:isInit?'':month,
      }
      api.curlPost('?cla=canteenApp&fun=statistics',para).then((res)=>{
        that.setData({
          dayArr:res.data.result.list,
          selected_month:api.formatDateCRM('Y年m月',res.data.result.month),
          formate_selected_month:api.rowTocol(res.data.result.month),
          formate_selected_date:res.data.result.date,
          meal_collection:res.data.result.statistics,
          xData:res.data.result.xData,
          yData:res.data.result.yData,
          hallName:res.data.result.hallName,
          hallId:res.data.result.hallId,
        });
        that.initCharts();
       
      })
    }
  },
  //步进器
  onStepChange(event){
    let num = event.detail;
    let id = event.currentTarget.dataset.id;
    let arr = this.data.menu;
    this.data.menu.forEach((v,k) => {
      if(v.id == id){
        arr[k].num = num;
      }
    });
    this.setData({
      menu:arr
    })
  },

  //报餐提交
  reportCommit(){
    let that = this;
    let para = {
      hallId:this.data.person.hallId,
      date:this.data.formate_selected_date,
      menu:JSON.stringify(this.data.menu),
      mealType:this.data.nowMealType,
      type:'报餐'
    }
    api.curlPost('?cla=canteenApp&fun=reportEdit',para).then((res)=>{
       that.requestData();
    })
  },
  detailTo(e){
    let mealType = e.currentTarget.dataset.type;
    console.log(mealType)
    wx.navigateTo({
      url: '../hallStatistics/hallStatistics?recordDate='+this.data.formate_selected_date+'&hallId='+this.data.hallId+'&mealType='+mealType,
    })

  },
  /**选择食堂 */
  chooseHall(){
    let that = this;
    api.curlPost('?cla=canteenApp&fun=replaceReportSearch',{type:'all'}).then((res)=>{
      that.setData({
        hall_popup_show:true,
        hallOption:res.data.hallOption
      })
    })
  },
    /**确认 */
    onHallConfirm(event){
      const { picker, value, index } = event.detail;
      this.setData({
        hallName:value.name,
        hallId:value.id,
        hall_popup_show:false
      });
      this.requestData();
    },
    onHallCancel(e){
      this.setData({
        hall_popup_show:false
      })
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})